package com.fyp.client.ptma.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fyp.client.ptma.ApplicationDetailFragment.ApplicationDetailClickListener;
import com.fyp.client.ptma.ApplicationsList;
import com.fyp.client.ptma.R;
import com.fyp.client.ptma.model.Application;
import com.fyp.client.ptma.model.ApplicationVersion;

public class CustomAdapterApplicationDetail extends BaseAdapter
{

	private List<ApplicationVersion> apps = new ArrayList<ApplicationVersion>();
	private Application application;
	private Context context;
	LayoutInflater mInflater;

	public CustomAdapterApplicationDetail(Context context)
	{
		this.context = context;

		mInflater = LayoutInflater.from(context);

	}

	public void setData(Application data)
	{

		if (data != null && data.getApplicationVersions().size() > 0)
		{

			application = data;
			apps.clear();

			for (ApplicationVersion appEntry : data.getApplicationVersions())
			{
				apps.add(appEntry);
			}
			notifyDataSetChanged();
		}

	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return apps.size();
	}

	@Override
	public ApplicationVersion getItem(int arg0)
	{
		// TODO Auto-generated method stub
		return apps.get(arg0);
	}

	@Override
	public long getItemId(int arg0)
	{

		return 0;
	}

	private class ViewHolder
	{
		TextView tvAppVersion;
		ImageView ivLog;
		TextView tvComment;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup vg)
	{
		final ViewHolder holder;
		if (convertView == null)
		{

			convertView = mInflater.inflate(R.layout.application_detail_row,
					null);

			holder = new ViewHolder();
			holder.ivLog = (ImageView) convertView.findViewById(R.id.ivLog);
			holder.tvAppVersion = (TextView) convertView
					.findViewById(R.id.tvAppVersion);

			holder.tvComment = (TextView) convertView
					.findViewById(R.id.tvComment);

			convertView.setTag(holder);
		} else
		{

			holder = (ViewHolder) convertView.getTag();
		}
		ApplicationVersion app = apps.get(position);

		holder.ivLog.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				ApplicationVersion version = getItem(position);
				if (version.getVersionPk().getApplication() == null)
				{
					application
							.setLatestVersion(application
									.getApplicationVersions()
									.get(application.getApplicationVersions()
											.size() - 1).getVersionPk()
									.getVersion());
					version.getVersionPk().setApplication(application);
				}

				((ApplicationDetailClickListener) context)
						.onApplicationVersionLogClickListener(version);

			}

		});
		holder.tvAppVersion.setText(app.getVersionPk().getVersion());

		holder.tvAppVersion.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				ApplicationVersion version = getItem(position);
				if (version.getVersionPk().getApplication() == null)
				{
					application
							.setLatestVersion(application
									.getApplicationVersions()
									.get(application.getApplicationVersions()
											.size() - 1).getVersionPk()
									.getVersion());
					version.getVersionPk().setApplication(application);
				}

				((ApplicationsList) context)
						.onApplicationVersionClickListener(version);

			}

		});

		if (application.getLatestVersion().equals(
				getItem(position).getVersionPk().getVersion()))
		{
			holder.tvComment.setVisibility(View.VISIBLE);
			holder.tvComment.setText("Latest");
		} else
		{
			holder.tvComment.setVisibility(View.GONE);
		}

		return convertView;
	}
}
